package com.newworld.dmsapp2.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.newworld.dmsapp2.BaseActivity;
import com.newworld.dmsapp2.LoginActivity;
import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.bean.User;
import com.newworld.dmsapp2.bean.UserAccount;
import com.newworld.dmsapp2.util.Config;
import com.newworld.dmsapp2.util.HttpUtil;
import com.newworld.dmsapp2.util.MyPreference;
import com.newworld.dmsapp2.util.SingleSample;
import com.newworld.dmsapp2.util.Tool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;



public class SettingAccountPasswordActivity extends BaseActivity implements
		OnClickListener {
	private ImageButton titlebar_both_left_modify_password_return;
	private Button titlebar_both_right_modify_password_save;
	private User user;
	private ProgressBar logInbar;
	private EditText oldpwd,newpwd,newpwdagin;
	protected Context context;
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Config.HANDLER_MSG_CHANGEPASSWORD_SUCCESS://修改密码成功
				logInbar.setVisibility(View.GONE);
				showShortToast((String) msg.obj);
				break;
			case Config.HANDLER_MSG_CHANGEPASSWORD_FAIL://修改密码失败
				logInbar.setVisibility(View.GONE);
				showShortToast((String) msg.obj);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_account_password);
		initViews();
		initEvents();
		
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.titlebar_both_left_modify_password_return:
			onBackPressed();
			break;
		case R.id.titlebar_both_right_modify_password_save:
			if(!oldpwd.getText().toString().equals("")&&!newpwd.getText().toString().equals("")&&!newpwdagin.getText().toString().equals("")){
				if(newpwd.getText().toString().equals(newpwdagin.getText().toString())){
					savepwd();
				}else{
					showShortToast("新密码校验不一致");
				}
			}else{
				showShortToast("信息未填写完整!");
			}
			//onBackPressed();
			break;
		default:
			break;
		}
	}
	
	private void savepwd(){
		if (Tool.isNetworkAvailable(SettingAccountPasswordActivity.this)) {
			try {
				String url = Tool.getConfigUrl(Config.SERVER_URL ,Config.EDIT_PASSWORD_URL);
						//Config.EDIT_PASSWORD_URL;
				String result = null;
				Map<String, String> map = new HashMap<String, String>();
				map.put("password", LoginActivity.encryption(oldpwd.getText().toString()));
				map.put("newpassword", LoginActivity.encryption(newpwd.getText().toString()));
				Log.e("新旧密码是", oldpwd.getText().toString()+"---"+newpwd.getText().toString());
				String setsessionid = "";
				user = SingleSample.getInstance(SettingAccountPasswordActivity.this).getUser();
				if(null != user){
					if(null != user.getSessionId() && !user.getSessionId().equals("")){
						setsessionid = user.getSessionId();
					}
				}
				result = HttpUtil.postRequestSe(url, map,setsessionid);
				Log.e("上传个人设置的result", result);
				String message = new JSONObject(result).getString("msg");
				String flag = new JSONObject(result).getString("success");
				if (flag.equals(Config.CONNECT_SUCCESS)) {
					MyPreference.getInstance(context).setUserId(0);
					Intent intent = new Intent();
					intent.setClass(SettingAccountPasswordActivity.this,
							LoginActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					mHandler.obtainMessage(
							Config.HANDLER_MSG_CHANGEPASSWORD_SUCCESS, message)
							.sendToTarget();// 通知主线程数据发送成功

				}else{
					mHandler.obtainMessage(
							Config.HANDLER_MSG_CHANGEPASSWORD_FAIL, message)
							.sendToTarget();// 通知主线程数据发送失败
				}

			} catch (Exception e) {
				
				Log.e("SettingAccountPasswordActivity", "json数据获取报错");
				mHandler.obtainMessage(
						Config.HANDLER_MSG_CHANGEPASSWORD_FAIL, "json数据获取报错")
						.sendToTarget();// 通知主线程数据发送失败
				e.printStackTrace();
			}
		}else {
			String message = "请设置网络";
			mHandler.obtainMessage(Config.HANDLER_MSG_CHANGEPASSWORD_FAIL,
					message).sendToTarget();
		}
	}

	@Override
	protected void initViews() {
		// TODO 自动生成的方法存根
		oldpwd = (EditText)findViewById(R.id.oldpwd);
		newpwd = (EditText)findViewById(R.id.newpwd);
		newpwdagin = (EditText)findViewById(R.id.newpwdagin);
		titlebar_both_left_modify_password_return = (ImageButton) findViewById(R.id.titlebar_both_left_modify_password_return);
		titlebar_both_right_modify_password_save = (Button) findViewById(R.id.titlebar_both_right_modify_password_save);
		logInbar = (ProgressBar)findViewById(R.id.setting_account_progressBar2);
	}

	@Override
	protected void initEvents() {
		// TODO 自动生成的方法存根
		titlebar_both_left_modify_password_return.setOnClickListener(this);
		titlebar_both_right_modify_password_save.setOnClickListener(this);
	}

}
